package medium最大子串;

import org.testng.annotations.Test;

import java.util.HashSet;
import java.util.Set;

public class Solution {
    public static int lengthOfLongestSubstring(String s) {
        int n = s.length();
        int ans = 0;
        Set<Character> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j <= n; j++) {
                String str = s.substring(i, j);
                char[] chars = str.toCharArray();
                for (char c : chars) {
                    set.add(c);
                }
                //相等意味着str没有重复元素
                if (set.size() == str.length()) {
                    ans = Math.max(ans, str.length());
                }else{
                    //已经存在重复元素，j再怎么变化依旧存在重复元素，直接结束这个循环
                    set.clear();
                    break;
                }
            }
        }
        return ans;
    }

    @Test
    public void test(){
        String s = "abcabcbb";
        int i = lengthOfLongestSubstring(s);
        System.out.println(i);
    }
}
